import {message} from "antd";
import {AxiosGet, AxiosPut} from "./HttpService";
import Config, {configChange} from "../Config";

let project: any;
configChange((config: any) => {
    project = config.project;
});

// @ts-ignore
const openApiLogsUrl: Function = function () {
    let newWindow = window.parent ? window.parent : window;
    newWindow.open(window.location.origin + "/apiLogs/" + project.id, "apiLogs");
}

// @ts-ignore
const openEditUrl: Function = function (menuId: string) {
    let newWindow = window.parent ? window.parent : window;
    newWindow.open(newWindow.location.origin + "/editmenu/" + menuId, "editMenu");
}

// @ts-ignore
const loadMenu: Function = async function (params: string) {
    let menus: any = (await AxiosGet("/sf-drs/platform/menus", params)).data;
    return menus;
}
// @ts-ignore
const loadVirtualMenu: Function = async function (params: string) {
    let menus: any = (await AxiosGet("/sf-drs/platform/menus/getVirtualChildren", params)).data || [];
    return menus;
}

// @ts-ignore
const loadMenuChild: Function = async function (params: string) {
    let menus: any = (await AxiosGet("/sf-drs/platform/menus/children", params)).data;
    return menus || [];
}

// @ts-ignore
const updateMenu: Function = async function (params: any) {
    const {data}: { data: any, code: string, msg: string } = await AxiosPut("/sf-drs/platform/menus", {menus: [params]});
    return data;
};

export {
    loadMenu,
    loadVirtualMenu,
    loadMenuChild,
    updateMenu,
    openApiLogsUrl,
    openEditUrl
};